package com.coocaa.launcher.util;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.view.ViewGroup;

import com.coocaa.x.framework.utils.ImageUtils;
import com.coocaa.x.framework.utils.LogUtils;
import com.coocaa.x.uipackage.utils.CoocaaFresco;
import com.coocaa.x.uipackage.widget.CoocaaDraweeView;
import com.facebook.drawee.controller.BaseControllerListener;
import com.facebook.drawee.controller.ControllerListener;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.image.ImageInfo;
import com.facebook.imagepipeline.request.BasePostprocessor;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
import com.facebook.imagepipeline.request.Postprocessor;
import com.tianci.media.api.Log;

/**
 * Created by luwei on 15-7-13.
 */
public class FrescoBitmapFactory {
    public interface OnBitmapLoadListener {
        public void loadSuccess(Bitmap bitmap, String url);

        public void loadFailed(String url);
    }

    public static void decodeBitmap(final Context context, final Uri uri, final int width, final int height, final OnBitmapLoadListener listener) {
        final CoocaaDraweeView view = new CoocaaDraweeView(context);
        final ViewGroup vg = (ViewGroup) ((Activity) context).findViewById(android.R.id.content);
        Postprocessor postprocessor = new BasePostprocessor() {
            @Override
            public void process(Bitmap bitmap) {
                LogUtils.d("wallpaper", "get bitmap url is " + uri.toString());
                if (bitmap != null) {
                    if (bitmap.getWidth() != width)
                        bitmap = ImageUtils.scale(bitmap, width, height);
                    if (listener != null) {
                        listener.loadSuccess(bitmap, uri.toString());
                    }
                }
                ((Activity) context).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        vg.removeView(view);
                    }
                });
            }
        };
        ControllerListener controllerListener = new BaseControllerListener<ImageInfo>() {
            @Override
            public void onFailure(String id, Throwable throwable) {
                LogUtils.d("fresco", "failed to load " + uri.toString());
                if (listener != null) {
                    listener.loadFailed(uri.toString());
                }
            }
        };
        ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri)
                .setPostprocessor(postprocessor)
                .setResizeOptions(new ResizeOptions(width, height))
                .build();
        DraweeController controller = CoocaaFresco.newDraweeControllerBuilder(context)
                .setControllerListener(controllerListener)
                .setOldController(view.getController())
                .setImageRequest(request)
                .build();
        view.setController(controller);
        ((Activity) context).runOnUiThread(new Runnable() {
            @Override
            public void run() {
                ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(0, 0);
                vg.addView(view, params);
            }
        });
    }
}
